package com.tomas.demo.controller.fastdfs;

import com.github.tobato.fastdfs.FdfsClientConfig;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.github.tobato.fastdfs.service.GenerateStorageClient;
import com.tomas.demo.common.jsonResult.ApiResult;
import com.tomas.demo.common.utils.FileUtils;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.util.Map;

/**
 * @author Tomas
 * @date 16/4/9 下午7:26
 * @what
 */
@Log4j
@RestController
@RequestMapping(value = "/dfs")
@Import(FdfsClientConfig.class)
public class FastDFSController {


    @Value("${deafult.images.host}")
    private  String IMAGEHOST;


    // 从 application.properties 中读取配置，如取不到默认值为Hello Shanhy
    @Value("${application.hello}")
    private String hello = "Hello Shanhy";

    @Autowired
    FastFileStorageClient fastFileStorageClient;
    @Autowired
    GenerateStorageClient defaultGenerateStorageClient;

    @RequestMapping()
    @ResponseBody
    public ApiResult index(Map<String, Object> model) {

        try {
            File tt=new File("/Users/user/github/springboot-swagger2/aaa.txt");
            //默认group1   inputStream 文件流 ,long 文件大小 , String 文件后缀 , Set<MateData> 附加信息
            StorePath storePath = fastFileStorageClient.uploadFile(new FileInputStream(tt), tt.length(), FileUtils.getFileExt(tt.getName()), null);
            log.info(IMAGEHOST+storePath.getGroup()+"/"+storePath.getPath());
           // return ApiResult.ok("ok",IMAGEHOST+storePath.getGroup()+"/"+storePath.getPath());

            //group 默认group1, inputStream 文件流 ,long 文件大小 , String 文件后缀
            StorePath txt = defaultGenerateStorageClient.uploadFile(null, new FileInputStream(tt), tt.length(),  FileUtils.getFileExt(tt.getName()));
            return ApiResult.ok("ok",IMAGEHOST+txt.getGroup()+"/"+txt.getPath());
        }catch (Exception e){
            log.error(e.getMessage());
            return new ApiResult("400","请求参数有误");
        }
    }
    @RequestMapping(value ="/hello")
    @ResponseBody
    public String hello(Map<String, Object> model) {

        return hello+"--"+"上午好";
    }



}